package edu.bluebeanforum.service.impl;

import edu.bluebeanforum.common.AppResult;
import edu.bluebeanforum.common.ResultCode;
import edu.bluebeanforum.exception.ApplicationException;
import edu.bluebeanforum.mapper.MessageMapper;
import edu.bluebeanforum.model.Message;
import edu.bluebeanforum.model.User;
import edu.bluebeanforum.service.IMessageService;
import edu.bluebeanforum.service.IUserService;
import edu.bluebeanforum.utils.StringUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.tags.EditorAwareTag;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        //非空校验
        if(message==null||message.getPostUserId()==null||
                message.getReceiveUserId()==null|| StringUtil.isEmpty(message.getContent())){
            log.info(ResultCode.FAILED_PARAMS_VALID.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));
        }
        //校验接受者是否存在
        User user=userService.selectById(message.getReceiveUserId());
        if(user==null||user.getDeleteState()==1){
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date=new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        int row = messageMapper.insertSelective(message);
        if (row!=1) {
            log.info(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public Message selectById(Long id) {
        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALID.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //非空校验
        if(receiveUserId==null||receiveUserId<=0){
            log.info(ResultCode.FAILED_PARAMS_VALID.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if (count==null) {
            log.info(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //非空校验
        if(receiveUserId==null||receiveUserId<=0){
            log.info(ResultCode.FAILED_PARAMS_VALID.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //非空校验
        if(id==null||id<=0||state<0||state>2){
            log.info(ResultCode.FAILED_PARAMS_VALID.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));
        }
        //构造更新对象
        Message updateMessage=new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date=new Date();
        updateMessage.setUpdateTime(date);
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row!=1) {
            log.info(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void reply(Long repliedId, Message message) {
        //非空校验
        if(repliedId==null||repliedId<=0){
            log.info(ResultCode.FAILED_PARAMS_VALID.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));
        }
        //校验repliedId对应的站内信状态
        Message existsMessage=messageMapper.selectByPrimaryKey(repliedId);
        if(existsMessage==null||existsMessage.getDeleteState()==1){
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态为已回复
        updateStateById(repliedId, (byte) 2);
        //回复的内容写入数据库
        create(message);

    }
}
